/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bbqtorrent;

import interfacegraphique.FenetrePrincipale;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import network.GererServeur;

/**
 *
 * @author dimitri
 */
public class Main {

	/**
	 * @param args the command line arguments
	 * @throws NoSuchAlgorithmException 
	 */
	public static void main(String[] args) {
		String path = "/home/dimitri/Bureau/irc.png.torrent";
		

		TorrentManager tm = new TorrentManager();

		try {			
			System.out.println(tm.addTorrent(path));			
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFoundException");
		} catch (IOException e) {
			System.out.println("IOException");
		} catch (InvalidBEncodingException e) {			
			System.out.println("InvalidBEncodingException");
		} catch (Exception e) {
			System.out.println("UnknownException");
			e.printStackTrace();
		}
                GererServeur serveurs = new GererServeur(tm);
                serveurs.start();


		//GererServeur gs = new GererServeur();
		//gs.start();

//		File file = new File("/home/dimitri/otaku-loader.tar.bz2");
//
//		int pieceLength = 65536;
//
//		HashMap hm = new HashMap<Object, Object>();
//		HashMap info = new HashMap<Object, Object>();
//		hm.put("announce", "http://localhost:9999/announce");
//		info.put("piece length", pieceLength);
//		info.put("name", file.getName());
//		info.put("length", file.length());
//
//		int nbPieces = (int) Math.ceil((double)file.length()/pieceLength);
//
//		System.out.println(file.getName()+" : "+file.length()+" ; "+nbPieces);
//
//		try {
//			FileInputStream fin = new FileInputStream(file);
//			byte[] buffer = new byte[pieceLength];
//			byte[] pieces = new byte[20*nbPieces];
//
//			MessageDigest cript;
//
//			int cpt = 1;
//			int pointeur = 0;
//
//			while(fin.read(buffer) != -1) {
//				System.out.println("nbpiece:"+cpt);
//
//				cript = MessageDigest.getInstance("SHA-1");
//				cript.reset();
//				cript.update(buffer);				
//				
//				System.arraycopy(cript.digest(), 0, pieces, pointeur, 20); 
//				
//				pointeur += 20;
//				cpt++;
//			}
//			
//			info.put("pieces", pieces);
//
//			hm.put("info", info);
//		} catch (FileNotFoundException e3) {
//			// TODO Auto-generated catch block
//			e3.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (NoSuchAlgorithmException e2) {
//			// TODO Auto-generated catch block
//			e2.printStackTrace();
//		} 
//		
//		FileOutputStream os;
//		
//		try {
//			os = new FileOutputStream("/home/dimitri/"+file.getName()+".torrent");			
//			Bencoder.bencode(hm, os);
//
//		} catch (FileNotFoundException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();				
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

//		FenetrePrincipale fp = new FenetrePrincipale();
	}
}
